package com.lmq.wxshare.controller;

import com.lmq.wxshare.common.HttpRequest;
import com.lmq.wxshare.common.ProduceCommon;
import com.lmq.wxshare.common.WXCommon;
import com.lmq.wxshare.constants.WxConstants;
import com.lmq.wxshare.entity.AccessToken;
import com.lmq.wxshare.entity.Produce;
import com.lmq.wxshare.entity.User;
import com.lmq.wxshare.service.AccessTokenService;
import com.lmq.wxshare.service.OrderService;
import com.lmq.wxshare.service.ProduceService;
import com.lmq.wxshare.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/wx")
public class WxController extends BaseController{

    @Autowired
    private WxConstants wxConstants;
    @Autowired
    private UserService userService;
    @Autowired
    private ProduceService produceService;
    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private OrderService orderService;


    /*拉取个人信息之后打开红包*/
    @RequestMapping("/getAccessTokenByCode")
    public String getAccessTokenByCode(String code, String state, Model model){
        String accessTokenUrl = wxConstants.getACCESS_TOKEN_URL().replace("APPID", wxConstants.getAppID())
                .replace("SECRET", wxConstants.getAppsecret())
                .replace("CODE", code);

        /**
         * 这里授权并更新或者插入用户信息
         */
        String result = HttpRequest.sendPost(accessTokenUrl,null);
        User user =  userService.insertOrUpdateByJson(result);
        user = userService.getUserDetail(user);
        userService.update(user, null);

        /**
         * 插入session
         */
        request.getSession().setAttribute("user", user);

        /**
         * 产品信息
         */
        Produce produce = produceService.getById(state);
        Integer redpacket =  ProduceCommon.getRedPacket(produce.getPrice());

        /*js页面api授权*/
        AccessToken token = accessTokenService.getAccessToken();
        String noncestr = WXCommon.getuuid();
        String timestamp = Long.toString(System.currentTimeMillis() / 1000);
        String url = request.getRequestURL()+"?code="+code+"&state="+state;
        String signature = accessTokenService.getSignature(token, noncestr, timestamp, url);

        //保存用户订单关系表
        orderService.saveOrupdte(user.getId(), produce.getId(), redpacket);

        model.addAttribute("user", user);
        model.addAttribute("redpacket", redpacket);
        model.addAttribute("noncestr", noncestr);
        model.addAttribute("timestamp", timestamp);
        model.addAttribute("signature", signature);
        model.addAttribute("appid", wxConstants.getAppID());
        return "redpacket/redpacket";
    }

    @RequestMapping("/share")
    public String shareUrlAndSaveUser(String code, String state, Model model){
        String accessTokenUrl = wxConstants.getACCESS_TOKEN_URL().replace("APPID", wxConstants.getAppID())
                .replace("SECRET", wxConstants.getAppsecret())
                .replace("CODE", code);
        /**
         * 这里授权并更新或者插入用户信息
         */
        String result = HttpRequest.sendPost(accessTokenUrl,null);
        User user =  userService.insertOrUpdateByJson(result);
        user = userService.getUserDetail(user);
        userService.update(user, null);
        /**
         * 插入session
         */
        request.getSession().setAttribute("user", user);
        model.addAttribute("userid", state);
        return "redpacket/openredpacket";
    }

    @RequestMapping("/friensRedpacket")
    public String friensRedpacket(String code, String state, Model model){
        /**
         * Code为将要帮忙拆红包的用户id
         */
        int redpacket = ProduceCommon.getRedPacketToFriend();



        return "";
    }

}
